// Copyright (C)  Will Coleda 2008

Ext.BLANK_IMAGE_URL = '/js/ext-2.0/resources/images/default/s.gif';

var cm = new Ext.grid.ColumnModel ([
  {id: 'id', header: 'Ticket', width: 30, sortable: true, dataIndex: 'id'},
  {header: 'Queue', width: 30, sortable: true, dataIndex: 'queue'},
  {header: 'Status', width: 30, sortable: true, dataIndex: 'status'},
  {header: 'Priority', width: 30, sortable: true, dataIndex: 'priority'},
  {header: 'Last Updated', width: 80, sortable: true, dataIndex: 'lastupdated'},
  {header: 'Subject', width: 150, dataIndex: 'subject'},
  {header: 'Summary', width: 200, dataIndex: 'summary'}
]);

function open_tab (name) {
  var ds = new Ext.data.JsonStore({
    url: './data/' + name + '.json',
    root: 'tickets',
    fields: [ 'id', 'queue', 'status','priority','lastupdated','subject','summary']
  });

  ds.load();

  var tp = Ext.getCmp('tabby');

  var tab = tp.add({
    closable: true,
    title: name,
    xtype: 'grid',
    ds: ds,
    cm: cm,
    viewConfig: { forceFit: true },
    sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
    width:600,
    enableColumnMove: true,
    enableColumnResize: true,
    stripeRows: true,
    viewConfig: {
      forceFit: true
    },
    listeners: {
      'rowdblclick':
      function(grid, row, event) {
        var ticket = grid.getStore().getAt(row).get('id');
        window.open('http://rt.perl.org/rt3/Public/Bug/Display.html?id=' + ticket ,'_blank');
      }
    }
  });

  tp.activate(tab);
}

Ext.onReady(function() {

  var first_query = null;
  {
    hash_pos = document.URL.indexOf("#");
    if (hash_pos != -1) {
      first_query = document.URL.substring(hash_pos+1);
    }
  }

  var panel = new Ext.Viewport ({
    layout: 'border',
    items: [{
      region: 'north',
      html: '<h1>Perl6/Parrot RT interface</h1>',
        autoHeight: true,
        border: false,
        margins: '0 0 5 0'
    }, {
      region: 'west',
      collapsible: true,
      title: 'Queries',
      xtype: 'treepanel',
      width: 150,
      autoScroll: true,
      split: true,
      loader: new Ext.tree.TreeLoader(),
      root: new Ext.tree.AsyncTreeNode({
        expanded: true,
        children: [{
          text: 'HLLs',
          expanded: true,
          children: [{
            text: 'perl6',
            leaf: true
          }, {
            text: 'tcl',
            leaf: true
          }]
        }, {
          text: 'Skillsets',
          expanded: true,
          children: [{
            text: 'Perl',
            leaf: true
          }, {
            text: 'C',
            leaf: true
          }, {
            text: 'PIR',
            leaf: true
          }]
        }, {
          text: 'Tags',
          expanded: true,
          children: [{
            text: 'CAGE',
            leaf: true
          }, {
            text: 'TODO',
            leaf: true
          }, {
            text: 'PATCH',
            leaf: true
          }, {
            text: 'DEPRECATED',
            leaf: true
          }, {
            text: 'BUG',
            leaf: true
          }, {
            text: 'RFC',
            leaf: true
          }, {
            text: 'RFE',
            leaf: true
          }, {
            text: 'PROPOSAL',
            leaf: true
          }]
        }]
      }),
      rootVisible: false,
      listeners: {
        click: function(n) {
          if (! n.isLeaf())
            return;

          open_tab(n.attributes.text);
        }
      }
    }, {
      id: 'tabby',
      region: 'center',
      xtype: 'tabpanel',
      resizeTabs: true,
      minTabWidth: 115,
      enableTabScroll: true,
      activeTab: 0,
      items: [{
        title: 'Instructions',
        html: 'Click on queries to the left to open new tabs. Each new tab ' +
          'will contain a list of tickets for that query.<br><br>' +
          'From the grid, double click on a row to open the corresponding ' +
          'page for that ticket.',
        closable: true
      }]
    }, {
      region: 'south',
      title: 'TODO',
      collapsible: true,
      html: '<ul>' + 
        '<li>Display summary like the feed example on ext.js site' +
        '<li>Setup a cron job to load this data every hour or so.' +
        '<li>only load the parts of ext we are using.' +
        '<li>Clicking on a ticket should open it in a tab, not a new window.' +
        '</ul>'
    }]
  });

  if (first_query != null)
    open_tab(first_query);

});

// vim: expandtab shiftwidth=2:
